package uoc.biblioteca.mappings.dao;

// Generated 11/04/2011 14:10:24 by Hibernate Tools 3.2.1.GA

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import uoc.biblioteca.mappings.object.HistoricRevistaVo;
import uoc.biblioteca.sessionFactory.HibernateSessionFactory;

/**
 * Home object for domain model class HistoricRevista.
 * @see uoc.biblioteca.mappings.dao.HistoricRevistaVo
 * @author Hibernate Tools
 */
public class HistoricRevistaHome extends BaseHome {

	private static final Log log = LogFactory.getLog(HistoricRevistaHome.class);

	private final SessionFactory sessionFactory = null;
	
	public void saveNew(HistoricRevistaVo instance) {
		
		log.debug("save new HistoricRevistaVo instance");
		
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			session.clear();
			
			tx = session.beginTransaction();
			
			session.saveOrUpdate(instance);
			
			tx.commit();
			session.close();
			
			log.debug("saveNew successful");
			
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	//Mostrar todos el historico de un libro determinado
	public HistoricRevistaVo[] getAllHistoricsBySolicitud(String idSolicitud) {
		
		/* will hold the books we are going to return later */
		try{
			List solicituds = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.HistoricRevistaVo as c where c.solicitudRevista.idSolicitud = "+idSolicitud;
			hql += " order by c.dataInici asc";
			
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				HistoricRevistaVo solicitudTemp = (HistoricRevistaVo) iter.next();
				solicitudTemp.getSolicitudRevista();
				solicitudTemp.getEstadoSolicitudByIdEstadoIni();
				solicitudTemp.getEstadoSolicitudByIdEstadoFi();
				solicituds.add(solicitudTemp);
			}
			tx.commit();
			
			return (HistoricRevistaVo[]) solicituds.toArray(new HistoricRevistaVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	
	
	
	
	
	
}
